package project

import (
	"context"
	"encoding/json"
	"fmt"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/remote/bigdata_remote"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/remote/opensearch_remote"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.myscrm.cn/golang/stark/v4"
	"strings"
	"time"
)

type BuildingService struct {
	bigDataServiceRepo bigdata_remote.BigDataServiceRepositoryIface
	openSearchRepos    opensearch_remote.OpenSearchRepositoryIface
}

func NewBuildingService(
	bigDataServiceRepo bigdata_remote.BigDataServiceRepositoryIface,
	openSearchRepos opensearch_remote.OpenSearchRepositoryIface,
) BuildingServiceIface {
	return &BuildingService{
		bigDataServiceRepo: bigDataServiceRepo,
		openSearchRepos:    openSearchRepos,
	}
}

func (s *BuildingService) BuildingList(ctx context.Context, request *pb.BuildingListRequest) (*pb.BuildingListResponse, error) {
	var newIsPriceGtAvg []string
	for _, v := range request.IsPriceGtAvg {
		if v == "高端项目" {
			v = "是"
		} else {
			v = "否"
		}
		newIsPriceGtAvg = append(newIsPriceGtAvg, v)
	}
	var organizingCodeArr []string
	if request.OrganizingCode != "" {
		organizingCodeArr, _ = s.bigDataServiceRepo.GetInvestCompanyCodes(ctx, request.OrganizingCode)
	}
	bigdataRequest := &opensearch_remote.BuildListRequest{
		PageSize:          request.PageSize,
		Page:              request.Page,
		Keyword:           request.Keyword,
		Area:              strings.Join(request.Area, ","),
		CompanyArea:       strings.Join(request.CompanyArea, ","),
		CompanyType:       strings.Join(request.CompanyType, ","),
		IsPriceGtAvg:      strings.Join(newIsPriceGtAvg, ","),
		PrjTypeNum:        strings.Join(request.PrjTypeNum, ","),
		PrjPropertyNum:    strings.Join(request.PrjPropertyNum, ","),
		StartDateStart:    request.StartDateStart,
		StartDateEnd:      request.StartDateEnd,
		CompleteDateStart: request.CompleteDateStart,
		CompleteDateEnd:   request.CompleteDateEnd,
		OrganizingCode:    strings.Join(organizingCodeArr, ","),
	}
	for _, v := range request.CompanyLabel {
		if v == constants.CompanyLabelDeveloperTop100Label {
			bigdataRequest.DeveloperTop100Label = v
		} else if v == constants.CompanyLabelNewDecorateExpandLabel {
			bigdataRequest.NewDecorateExpandLabel = v
		} else if v == constants.CompanyLabelNewLandUsageExpandLabel {
			bigdataRequest.NewLandUsageExpandLabel = v
		} else if v == constants.CompanyLabelNewAreaExpandLabel {
			bigdataRequest.NewAreaExpandLabel = v
		} else if v == constants.CompanyLabelPriceGtAvgLabel {
			bigdataRequest.PriceGtAvgLabel = v
		} else if v == constants.CompanyLabelUrbanInvestmentLabel {
			bigdataRequest.UrbanInvestmentLabel = v
		}
	}

	buildResponse, err := s.openSearchRepos.BuildList(ctx, bigdataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "opensearch BuildList,err:%s", err.Error())
	}
	resp := &pb.BuildingListResponse{
		List: make([]*pb.BuildingListData, 0),
	}

	resp.Total = buildResponse.Total
	for _, item := range buildResponse.List {
		row := &pb.BuildingListData{}
		row.Id = item.Id
		row.Address = item.Address
		row.City = item.City
		row.CityCode = item.CityCode
		row.StartDate = strings.Split(item.StartDate, " ")[0]
		row.CompleteDate = strings.Split(item.CompleteDate, " ")[0]
		row.PrjTypeNum = item.PrjTypeNum
		row.PrjPropertyNum = item.PrjPropertyNum
		row.PrjName = item.PrjName
		row.IsPriceGtAvg = item.IsPriceGtAvg
		if row.IsPriceGtAvg == "是" {
			row.IsPriceGtAvg = "高端项目"
		} else {
			row.IsPriceGtAvg = ""
		}
		row.BuildCorpName = item.BuildCorpName
		row.OrganizingCode = item.OrganizingCode
		row.CompanyTypeLabel = item.CompanyTypeLabel
		row.TopDeveloperName = item.TopDeveloperName
		row.TopOrganizingCode = item.TopOrganizingCode
		row.TopCompanyTypeLabel = item.TopCompanyTypeLabel
		row.ConscorpName = item.ConscorpName
		row.ConscorpOrganizingCode = item.ConscorpOrganizingCode
		row.County = item.County
		row.Province = item.Province
		row.ProvinceCode = item.ProvinceCode
		row.PrjName_HL = item.PrjNameHL
		row.BuildCorpName_HL = item.BuildCorpNameHL
		row.StartDateType = item.StartDateType

		allInvest, _ := utils.DecimalFloat(item.AllInvest)
		row.AllInvest = allInvest
		allArea, _ := utils.DecimalFloat(item.AllArea)
		row.AllArea = allArea

		resp.List = append(resp.List, row)
	}
	return resp, nil
}

func (s *BuildingService) GetBuildingDetailBasic(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailBasicResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id: request.Id,
	}
	buildingDetail, err := s.bigDataServiceRepo.GetBuildingDetailBasic(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBasic request:%s, error: %v", request.Id, err)
		return nil, err
	}

	resp := &pb.BuildingDetailBasicResponse{
		Id:                                 buildingDetail.Id,
		Title:                              buildingDetail.ProjectName,
		ProjectNum:                         buildingDetail.ProjectNum,
		ProjectProvinceNum:                 buildingDetail.ProjectProvinceNum,
		BuildCorpName:                      buildingDetail.BuildCorpName,
		BuildCorpOrganizingCode:            buildingDetail.BuildCorpOrganizingCode,
		UpdateTime:                         buildingDetail.UpdateTime,
		TotalInvestment:                    buildingDetail.TotalInvestment,
		TotalArea:                          buildingDetail.TotalArea,
		ProjectType:                        buildingDetail.ProjectType,
		ProjectCategory:                    buildingDetail.ProjectCategory,
		ConstructionNature:                 buildingDetail.ConstructionNature,
		DataLevel:                          buildingDetail.DataLevel,
		Address:                            buildingDetail.Address,
		StartDate:                          buildingDetail.StartDate,
		EndDate:                            buildingDetail.EndDate,
		ProjectApprovalNum:                 buildingDetail.ProjectApprovalNum,
		ProjectApprovalLevel:               buildingDetail.ProjectApprovalLevel,
		ProjectApprovalAuthority:           buildingDetail.ProjectApprovalAuthority,
		ProjectApprovalDate:                buildingDetail.ProjectApprovalDate,
		FundingSource:                      buildingDetail.FundingSource,
		StateOwnedCapitalContributionRatio: buildingDetail.StateOwnedCapitalContributionRatio,
		IsKeyProject:                       buildingDetail.IsKeyProject,
		ConstructionScale:                  buildingDetail.ConstructionScale,
		Province:                           buildingDetail.Province,
		City:                               buildingDetail.City,
		IsPriceGtAvg:                       buildingDetail.IsPriceGtAvg,
	}

	// 处理时间格式
	if resp.StartDate != "" {
		parsedTime, err := time.Parse("2006-01-02 15:04:05", resp.StartDate)
		if err == nil {
			resp.StartDate = parsedTime.Format("2006-01-02")
		}
	}
	if resp.EndDate != "" {
		parsedTime, err := time.Parse("2006-01-02 15:04:05", resp.EndDate)
		if err == nil {
			resp.EndDate = parsedTime.Format("2006-01-02")
		}
	}
	if resp.ProjectApprovalDate != "" {
		parsedTime, err := time.Parse("2006-01-02 15:04:05", resp.ProjectApprovalDate)
		if err == nil {
			resp.ProjectApprovalDate = parsedTime.Format("2006-01-02")
		}
	}
	// 拼接行政区划
	resp.AdministrativeArea = buildingDetail.Province + buildingDetail.City + buildingDetail.County

	relatedCompany := make([]*pb.Company, 0)
	// 组装相关企业建设单位
	if resp.BuildCorpName != "" {
		relatedCompany = append(relatedCompany, &pb.Company{
			CompanyName:       resp.BuildCorpName,
			OrganizingCode:    resp.BuildCorpOrganizingCode,
			TopCompanyName:    buildingDetail.TopDeveloperName,
			TopOrganizingCode: buildingDetail.TopOrganizingCode,
			BusinessType:      constants.RelatedCompanyTypeConstructionUnit,
			Labels:            strings.Split(buildingDetail.CompanyTypeLabel, ","),
		})
	}
	// 施工单位
	if buildingDetail.BuildUnit != "" {
		relatedCompany = append(relatedCompany, &pb.Company{
			CompanyName:    buildingDetail.BuildUnit,
			OrganizingCode: buildingDetail.BuildUnitOrganizingCode,
			BusinessType:   constants.RelatedCompanyTypeBuilder,
			Labels:         make([]string, 0),
		})
	}
	resp.RelatedCompany = relatedCompany

	return resp, nil
}

func (s *BuildingService) GetBuildingDetailTargetNum(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailTargetNumResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id: request.Id,
	}
	targets, err := s.bigDataServiceRepo.GetBuildingDetailTargetNum(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailTargetNum request:%s, error: %v", request.Id, err)
		return nil, err
	}
	return &pb.BuildingDetailTargetNumResponse{
		Bid:                  targets.Bid,
		Contract:             targets.Contract,
		WorkingDrawingAudit:  targets.WorkingDrawingAudit,
		ConstructionPermit:   targets.ConstructionPermit,
		CompletionAcceptance: targets.CompletionAcceptance,
		CompletionFiling:     targets.CompletionFiling,
		ParticipatingUnit:    targets.ParticipatingUnit,
		Contact:              targets.Contact,
	}, nil
}

func (s *BuildingService) GetBuildingDetailBidList(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailBidListResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	bidList, err := s.bigDataServiceRepo.GetBuildingDetailBidList(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBidList request:%s, error: %v", request.Id, err)
		return nil, err
	}
	response := &pb.BuildingDetailBidListResponse{
		Total: bidList.Total,
		List:  make([]*pb.BuildingDetailBidListItem, 0),
	}

	if bidList.List != nil && len(bidList.List) > 0 {
		for _, bid := range bidList.List {
			if bid.WinbidDate != "" {
				parsedTime, err := time.Parse("2006-01-02 15:04:05", bid.WinbidDate)
				if err == nil {
					bid.WinbidDate = parsedTime.Format("2006-01-02")
				}
			}
			response.List = append(response.List, &pb.BuildingDetailBidListItem{
				BidType:        bid.BidType,
				Id:             bid.Id,
				OrganizingCode: bid.OrganizingCode,
				PersonName:     bid.PersonName,
				ProjectId:      bid.ProjectId,
				ProjectNum:     bid.ProjectNum,
				TenderUnit:     bid.TenderUnit,
				WinbidAmount:   bid.WinbidAmount,
				WinbidDate:     bid.WinbidDate,
			})
		}
	}

	return response, nil
}

func (s *BuildingService) GetBuildingDetailParticipatingUnits(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailParticipatingUnitsResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	participatingUnits, err := s.bigDataServiceRepo.GetBuildingDetailParticipatingUnits(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailParticipatingUnits request:%s, error: %v", request.Id, err)
		return nil, err
	}
	response := &pb.BuildingDetailParticipatingUnitsResponse{
		Total: participatingUnits.Total,
		List:  make([]*pb.BuildingDetailParticipatingUnitsItem, 0),
	}

	if participatingUnits.List != nil && len(participatingUnits.List) > 0 {
		for _, participatingUnit := range participatingUnits.List {
			response.List = append(response.List, &pb.BuildingDetailParticipatingUnitsItem{
				CompanyRole:    participatingUnit.CompanyRole,
				Id:             participatingUnit.Id,
				OrganizingCode: participatingUnit.OrganizingCode,
				PersonName:     participatingUnit.PersonName,
				PersonPhone:    participatingUnit.PersonPhone,
				ProjectId:      participatingUnit.ProjectId,
				ProjectNum:     participatingUnit.ProjectNum,
				UnitName:       participatingUnit.UnitName,
			})
		}
	}

	return response, nil
}

func (s *BuildingService) GetBuildingDetailContractList(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailContractListResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	contractList, err := s.bigDataServiceRepo.GetBuildingDetailContractList(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailContractList request:%s, error: %v", request.Id, err)
		return nil, err
	}
	response := &pb.BuildingDetailContractListResponse{
		Total: contractList.Total,
		List:  make([]*pb.BuildingDetailContractListItem, 0),
	}
	if contractList.List != nil && len(contractList.List) > 0 {
		for _, contract := range contractList.List {
			response.List = append(response.List, &pb.BuildingDetailContractListItem{
				ContractAmount:                    contract.ContractAmount,
				ContractIssuingUnit:               contract.ContractIssuingUnit,
				ContractIssuingUnitOrganizingCode: contract.ContractingUnitOrganizingCode,
				ContractType:                      contract.ContractType,
				ContractingUnit:                   contract.ContractingUnit,
				ContractingUnitOrganizingCode:     contract.ContractIssuingUnitOrganizingCode,
				Id:                                contract.Id,
				ProjectId:                         contract.ProjectId,
				ProjectNum:                        contract.ProjectNum,
				SigningDate:                       contract.SigningDate,
				UpdateTime:                        contract.UpdateTime,
			})
		}
	}
	return response, nil
}

func (s *BuildingService) GetBuildingDetailWorkingDrawingAudit(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailWorkingDrawingAuditResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	workingDrawingAudits, err := s.bigDataServiceRepo.GetBuildingDetailWorkingDrawingAudit(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailWorkingDrawingAudit request:%s, error: %v", request.Id, err)
		return nil, err
	}
	response := &pb.BuildingDetailWorkingDrawingAuditResponse{
		Total: workingDrawingAudits.Total,
		List:  make([]*pb.BuildingDetailWorkingDrawingAuditItem, 0),
	}
	if workingDrawingAudits.List != nil && len(workingDrawingAudits.List) > 0 {
		for _, v := range workingDrawingAudits.List {
			response.List = append(response.List, &pb.BuildingDetailWorkingDrawingAuditItem{
				Id:                             v.Id,
				IsCensorWfqtCount:              v.IsCensorWfqtCount,
				IsFirstPass:                    v.IsFirstPass,
				ProjectId:                      v.ProjectId,
				ProjectNum:                     v.ProjectNum,
				ReviewAgencyUnit:               v.ReviewAgencyUnit,
				ReviewAgencyUnitOrganizingCode: v.ReviewAgencyUnitOrganizingCode,
				ReviewCompletionDate:           v.ReviewCompletionDate,
				UpdateTime:                     v.UpdateTime,
			})
		}
	}

	return response, nil
}

func (s *BuildingService) GetBuildingDetailConstructionPermit(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailConstructionPermitResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	constructionPermitList, err := s.bigDataServiceRepo.GetBuildingDetailConstructionPermit(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailConstructionPermit request:%s, error: %v", request.Id, err)
		return nil, err
	}
	response := &pb.BuildingDetailConstructionPermitResponse{
		Total: constructionPermitList.Total,
		List:  make([]*pb.BuildingDetailConstructionPermitItem, 0),
	}
	if constructionPermitList.List != nil && len(constructionPermitList.List) > 0 {
		for _, constructionPermit := range constructionPermitList.List {
			response.List = append(response.List, &pb.BuildingDetailConstructionPermitItem{
				Area:                           constructionPermit.Area,
				CertificateIssueDate:           constructionPermit.CertificateIssueDate,
				ConstructionUnit:               constructionPermit.ConstructionUnit,
				ConstructionUnitOrganizingCode: constructionPermit.ConstructionUnitOrganizingCode,
				ContractAmount:                 constructionPermit.ContractAmount,
				Id:                             constructionPermit.Id,
				ProjectId:                      constructionPermit.ProjectId,
				ProjectNum:                     constructionPermit.ProjectNum,
				SupervisionUnit:                constructionPermit.SupervisionUnit,
				SupervisionUnitOrganizingCode:  constructionPermit.SupervisionUnitOrganizingCode,
			})
		}
	}
	return response, nil
}

func (s *BuildingService) GetBuildingDetailCompletionFiling(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailCompletionFilingResponse, error) {
	// 查询数据
	bigDataRequest := &bigdata_remote.GetBuildingDetailRequest{
		Id:       request.Id,
		Page:     request.Page,
		PageSize: request.PageSize,
	}
	completionFilingList, err := s.bigDataServiceRepo.GetBuildingDetailCompletionFiling(ctx, bigDataRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailCompletionFiling request:%s, error: %v", request.Id, err)
		return nil, err
	}
	response := &pb.BuildingDetailCompletionFilingResponse{
		Total: completionFilingList.Total,
		List:  make([]*pb.BuildingDetailCompletionFilingItem, 0),
	}
	if completionFilingList.List != nil && len(completionFilingList.List) > 0 {
		for _, completionFiling := range completionFilingList.List {
			response.List = append(response.List, &pb.BuildingDetailCompletionFilingItem{
				ActualConstructionScale: completionFiling.ActualConstructionScale,
				ActualCost:              completionFiling.ActualCost,
				ActualStartDate:         completionFiling.ActualStartDate,
				FilingDate:              completionFiling.FilingDate,
				Id:                      completionFiling.Id,
				ProjectId:               completionFiling.ProjectId,
				ProjectNum:              completionFiling.ProjectNum,
				UpdateTime:              completionFiling.UpdateTime,
			})
		}
	}

	return response, nil
}

func (s *BuildingService) GetExportBuildingTotal(ctx context.Context, condition interface{}) (total int64, err error) {
	var request *pb.BuildingListRequest
	switch condition.(type) {
	case *pb.SaleListRequest:
		request = condition.(*pb.BuildingListRequest)
	case string:
		request = &pb.BuildingListRequest{}
		if condition.(string) != "" {
			err = json.Unmarshal([]byte(condition.(string)), request)
			if err != nil {
				stark.Logger.Errorf(ctx, "省市洞察 - 在建项目列表获取导出记录条数错误,condition:%+v, err:%s", condition, err)
				return
			}
		}
	}

	var newIsPriceGtAvg []string
	for _, v := range request.IsPriceGtAvg {
		if v == "高端项目" {
			v = "是"
		} else {
			v = "否"
		}
		newIsPriceGtAvg = append(newIsPriceGtAvg, v)
	}
	var organizingCodeArr []string
	if request.OrganizingCode != "" {
		organizingCodeArr, _ = s.bigDataServiceRepo.GetInvestCompanyCodes(ctx, request.OrganizingCode)
	}
	openSearchRequest := &opensearch_remote.BuildListRequest{
		PageSize:          1,
		Page:              1,
		Keyword:           request.Keyword,
		Area:              strings.Join(request.Area, ","),
		CompanyArea:       strings.Join(request.CompanyArea, ","),
		CompanyType:       strings.Join(request.CompanyType, ","),
		IsPriceGtAvg:      strings.Join(newIsPriceGtAvg, ","),
		PrjTypeNum:        strings.Join(request.PrjTypeNum, ","),
		PrjPropertyNum:    strings.Join(request.PrjPropertyNum, ","),
		StartDateStart:    request.StartDateStart,
		StartDateEnd:      request.StartDateEnd,
		CompleteDateStart: request.CompleteDateStart,
		CompleteDateEnd:   request.CompleteDateEnd,
		OrganizingCode:    strings.Join(organizingCodeArr, ","),
	}
	for _, v := range request.CompanyLabel {
		if v == constants.CompanyLabelDeveloperTop100Label {
			openSearchRequest.DeveloperTop100Label = v
		} else if v == constants.CompanyLabelNewDecorateExpandLabel {
			openSearchRequest.NewDecorateExpandLabel = v
		} else if v == constants.CompanyLabelNewLandUsageExpandLabel {
			openSearchRequest.NewLandUsageExpandLabel = v
		} else if v == constants.CompanyLabelNewAreaExpandLabel {
			openSearchRequest.NewAreaExpandLabel = v
		} else if v == constants.CompanyLabelPriceGtAvgLabel {
			openSearchRequest.PriceGtAvgLabel = v
		} else if v == constants.CompanyLabelUrbanInvestmentLabel {
			openSearchRequest.UrbanInvestmentLabel = v
		}
	}

	searchData, err := s.openSearchRepos.BuildList(ctx, openSearchRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetExportBuildingTotal BuildList request:%#v, err:%s", openSearchRequest, err.Error())
		return
	}

	if searchData == nil {
		return
	}

	total = int64(searchData.Total)
	return
}

func (s *BuildingService) QueryExportBuildingBatchData(ctx context.Context, condition interface{}, batchNum, batchSize int32) (rowTitle []string, rowData [][]string, err error) {
	rowTitle = strings.Split(constants.OrderTypeBuildingExportFields, "，")
	var request *pb.BuildingListRequest
	switch condition.(type) {
	case *pb.SaleListRequest:
		request = condition.(*pb.BuildingListRequest)
	case string:
		request = &pb.BuildingListRequest{}
		if condition.(string) != "" {
			err = json.Unmarshal([]byte(condition.(string)), request)
			if err != nil {
				stark.Logger.Errorf(ctx, "省市洞察 - 在建项目列表获取导出记录条数错误,condition:%+v, err:%s", condition, err)
				return
			}
		}
	}

	var newIsPriceGtAvg []string
	for _, v := range request.IsPriceGtAvg {
		if v == "高端项目" {
			v = "是"
		} else {
			v = "否"
		}
		newIsPriceGtAvg = append(newIsPriceGtAvg, v)
	}
	var organizingCodeArr []string
	if request.OrganizingCode != "" {
		organizingCodeArr, _ = s.bigDataServiceRepo.GetInvestCompanyCodes(ctx, request.OrganizingCode)
	}
	openSearchRequest := &opensearch_remote.BuildListRequest{
		PageSize:          batchSize,
		Page:              batchNum,
		Keyword:           request.Keyword,
		Area:              strings.Join(request.Area, ","),
		CompanyArea:       strings.Join(request.CompanyArea, ","),
		CompanyType:       strings.Join(request.CompanyType, ","),
		IsPriceGtAvg:      strings.Join(newIsPriceGtAvg, ","),
		PrjTypeNum:        strings.Join(request.PrjTypeNum, ","),
		PrjPropertyNum:    strings.Join(request.PrjPropertyNum, ","),
		StartDateStart:    request.StartDateStart,
		StartDateEnd:      request.StartDateEnd,
		CompleteDateStart: request.CompleteDateStart,
		CompleteDateEnd:   request.CompleteDateEnd,
		OrganizingCode:    strings.Join(organizingCodeArr, ","),
	}
	for _, v := range request.CompanyLabel {
		if v == constants.CompanyLabelDeveloperTop100Label {
			openSearchRequest.DeveloperTop100Label = v
		} else if v == constants.CompanyLabelNewDecorateExpandLabel {
			openSearchRequest.NewDecorateExpandLabel = v
		} else if v == constants.CompanyLabelNewLandUsageExpandLabel {
			openSearchRequest.NewLandUsageExpandLabel = v
		} else if v == constants.CompanyLabelNewAreaExpandLabel {
			openSearchRequest.NewAreaExpandLabel = v
		} else if v == constants.CompanyLabelPriceGtAvgLabel {
			openSearchRequest.PriceGtAvgLabel = v
		} else if v == constants.CompanyLabelUrbanInvestmentLabel {
			openSearchRequest.UrbanInvestmentLabel = v
		}
	}

	saleResponse, err := s.openSearchRepos.BuildList(ctx, openSearchRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "opensearch BuildList,err:%s", err.Error())
	}

	if saleResponse == nil || saleResponse.Total == 0 {
		return
	}

	//项目名称，工程用途，项目地区，开工时间，竣工时间，投资金额，施工面积，建设单位，顶级公司，施工单位，项目地址
	for _, item := range saleResponse.List {
		area := ""
		if item.Province == item.City {
			area = item.Province
		} else {
			area = fmt.Sprintf("%s%s", item.Province, item.City)
		}

		allInvest, _ := utils.DecimalFloat(item.AllInvest)
		allArea, _ := utils.DecimalFloat(item.AllArea)
		rowData = append(rowData, []string{
			item.PrjName,                             //项目名称，
			item.PrjTypeNum,                          //工程用途，
			area,                                     //项目地区，
			strings.Split(item.StartDate, " ")[0],    //开工时间，
			strings.Split(item.CompleteDate, " ")[0], //竣工时间
			fmt.Sprintf("%.2f万人民币", allInvest), //投资金额，
			fmt.Sprintf("%.2f平米", allArea),     //施工面积，
			item.BuildCorpName,                 //建设单位，
			item.TopDeveloperName,              //顶级公司，
			item.ConscorpName,                  //施工单位，
			item.Address,                       //项目地址
		})
	}

	return
}
